
import Cookie from'js-cookie'
export default {
    state:{
        isCollapse: false,   //控制菜单展开还是收起
        tabsList: [
            {
                path: "/",
                name: "home",
                label: "系统模型",
                icon: "s-home",
                url: "Home/Home",
            }
        ],//面包屑数据
        menu:[],
        showManagementSystem:false,
    },
    mutations: {
        //修改菜单展开或收起
        managementSystem(state,permission){
            if("管理员"===permission||"超级管理员"===permission){
                state.showManagementSystem=true
            }else{
                state.showManagementSystem=false
            }
        },
        //修改菜单展开或收起
        collapseMenu(state){
            state.isCollapse = !state.isCollapse;
        },
        //更新面包屑数据
        selectMenu(state,val) {
            if(val.name !=='home'){
                const index=state.tabsList.findIndex(item=>item.name===val.name)
                if(index === -1){
                    state.tabsList.push(val)
                }
            }else{
                state.tabsList=[
                    {
                        path: "/home",
                        name: "home",
                        label: "系统模型"
                    }
                ]
            }
        },
        closeTag(state,item) {
            const index = state.tabsList.findIndex(val=>val.name===item.name)
            state.tabsList.splice(index,1)
        },
        //设置menu的数据
        setMenu(state,val){
            state.menu = val
            Cookie.set('menu',JSON.stringify(val))
        },
        //动态注册路由
        addMenu(state, router) {
            console.log('addmenu')
            if(!Cookie.get('menu'))return;
            const menu = JSON.parse(Cookie.get('menu'));
            state.menu = menu;
            const menuArray = [];
            menu.forEach(item=>{
                if(item.children){
                    item.children = item.children.map(item=>{
                        item.component = ()=>import(`../views/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                }else{
                    item.component = ()=>import(`../views/${item.url}`)
                    menuArray.push(item)
                }
            })
            console.log(menuArray)
            // 路由动态添加
            console.log('before menuArray',router)
            menuArray.forEach(item=>{
                router.addRoute('main',item)
            })
            console.log('after menuArray',router)
         }
        
    },
    actions: {
        
    }
}